<?php 

if(!function_exists('cycle'))
{
	function cycle()
	{
		static $i;	
		
		if (func_num_args() == 0)
		{
			$args = array('even','odd');
		}
		else
		{
			$args = func_get_args();
		}
		return 'class="'.$args[($i++ % count($args))].'"';
	}
}
if(!function_exists('menu_active'))
{
	function menu_active($module, $controller = FALSE, $class='active')
	{
		$CI =& get_instance();
		if($controller)
		{
			$class = NULL;
			$method = NULL;
			@list($class, $method) = explode('/', $controller);
			if($method)
			{
				return ($CI->router->fetch_module() == $module && $CI->router->fetch_class() == $class && $CI->router->fetch_method() == $method) ? 'class='.$class : '';
			}
			else
			{
				return ($CI->router->fetch_module() == $module && $CI->router->fetch_class() == $class) ? 'class='.$class : '';	
			}	
		}
		else
		{
			return ($CI->router->fetch_module() == $module) ? 'class='.$class : '';	
		}
	}
}

if(!function_exists('menu_active2'))
{
	function menu_active2($module,$controller = FALSE,$class='active')
	{
		$CI =& get_instance();
		if($controller)
		{
			return ($CI->router->fetch_module() == $module && $CI->router->fetch_class() == $controller) ? 'class='.$class : '';	
		}
		else
		{
			return ($CI->router->fetch_module() == $module) ? 'class='.$class : '';	
		}
	}
}

if(!function_exists('page_active'))
{
	function page_active($page,$uri=4,$class='active')
	{
		$CI =& get_instance();
		return ($CI->uri->segment($uri)==$page) ? 'class='.$class : '';
	}
}

if(!function_exists('content_active'))
{
	function content_active($page,$uri=3,$class='active')
	{
		$CI =& get_instance();
		return ($CI->uri->segment($uri)==$page) ? 'class='.$class : '';
	}
}

if(!function_exists('option_publish'))
{
	function option_publish()
	{
		return array('on' => 'ON', 'off' => 'OFF');
	}
}

if(!function_exists('get_option'))
{
	function get_option($value,$text,$table,$condition = NULL,$lang = NULL)
	{
		$CI =& get_instance();
		$query = $CI->db->query("select * from $table $condition");
		foreach($query->result() as $item) $option[$item->{$value}] = lang_decode($item->{$text},$lang);
		return $option;
	}
}

function get_root($category_id)
{
	static $data;
	$category = new Category($category_id);
	$data = $category_id;
	if($category->parent_id<>0)
	{
		get_root($category->parent_id);
	}
	return $data;
}

function get_breadcrumb($category_id,$admin = FALSE)
{
	static $data;
	$path = ($admin)?'contents/admin/contents/index/':'contents/index/';
	$category = new Category($category_id);
	if(($category->layout_id!=5)||($category->structure_id!=2))
	{
		if($category->structure_id==1)
		{
			$data['home/'.$category->slug] = $category->name;
		}
		else
		{
			$data[$path.$category->id] = $category->name;
		}
	}
	if($category->parent_id<>0)
	{
		get_breadcrumb($category->parent_id,$admin);
	}
	return $data;
}

function get_category_breadcrumb($category_id)
{
	static $data;
	$path = 'contents/admin/categories/index/';
	$category = new Category($category_id);
	
	$data[$path.$category->id] = $category->name;

	if($category->parent_id<>0)
	{
		get_category_breadcrumb($category->parent_id);
	}
	return $data;
}

function breadcrumb($data)
{
	$data = array_reverse($data, true);
	$breadcrumb = '<div id="breadcrumb"><ul>';
	$arrow = '';
	foreach($data as $key => $value)
	{
		$breadcrumb .= $arrow.'<li>'.anchor($key,lang_decode($value)).'</li>';
		$arrow = '<span>»</span>';
	}
	$breadcrumb .= '</ul><div class="clear"></div></div><div class="clear"></div>';
	return $breadcrumb;
}

function admin_breadcrumb($data)
{
	$data = array_reverse($data, true);
	$breadcrumb = '';
	$arrow = '';
	foreach($data as $key => $value)
	{
		$breadcrumb .= $arrow.anchor($key,lang_decode($value));
		$arrow = '<span> » </span>';
	}
	return $breadcrumb;
}

function fix_file(&$files)
{
    $names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);

    foreach ($files as $key => $part) {
        // only deal with valid keys and multiple files
        $key = (string) $key;
        if (isset($names[$key]) && is_array($part)) {
            foreach ($part as $position => $value) {
                $files[$position][$key] = $value;
            }
            // remove old key reference
            unset($files[$key]);
        }
    }
}

function get_sitemap($orm)
{
	if($orm->child->id)
	{
		echo '<ul>';
		foreach($orm->child as $child)
		{
			echo '<li><a href="contents/index/'.$child->id.'">'.lang_decode($child->name).'</a>';
			get_sitemap($child);
			echo '</li>';
		}
		echo '</ul>';
		
	}
}

function remove_dir($dir) 
{ 
	if(is_dir($dir)) 
	{ 
		$dir = (substr($dir, -1) != "/")? $dir."/":$dir; $openDir = opendir($dir); 
		while($file = readdir($openDir)) 
		{ 
			if(!in_array($file, array(".", ".."))) 
			{ 
				if(!is_dir($dir.$file)) 
				{ 
					@unlink($dir.$file); 
				} 
				else 
				{ 
				remove_dir($dir.$file); 
				} 
			} 
		} 
		closedir($openDir); @rmdir($dir); 
	} 
} 

function icon_file($file)
{
	if($file)
	{
	return '<img src="media/kcfinder/themes/oxygen/img/files/small/'.pathinfo($file, PATHINFO_EXTENSION).'.png"  style="float:none; padding:0; border:none; vertical-align:top;"  />';
	}
	else
	{
		return false;
	}
}

if(!function_exists('tag_decode'))
{
	function tag_decode($tags)
	{
		$CI =& get_instance();
		$tags = explode(",",$tags);
		$link = "";
		$comma = "";
		foreach($tags as $tag)
		{
			$link .= $comma.'<a href="'.$CI->router->fetch_module().'/tag?tag='.$tag.'">'.$tag.'</a>';
			$comma = ", ";
		}
		return $link;
	}
}
if(!function_exists('avatar'))
{
	function avatar($img=FALSE,$size = NULL)
	{
		return ($img)?'uploads/users/'.$size.$img:'themes/default/images/'.$size.'noavatar.gif';
	}
}
function webboard_group($post,$type){
	$CI =& get_instance();
	$webboard_post_level = new Webboard_post_level();
	$webboard_post_level->where('post <',$post)->order_by('post','desc')->limit(1)->get();
	if($webboard_post_level->exists())
	{
		if($type == "name")
		{
			return $webboard_post_level->name;
		}
		else
		{
			return $webboard_post_level->image;
		}
	}
	else
	{
		$webboard_post_level->get_by_name('Starter');
		if($type == "name")
		{
			return $webboard_post_level->name;
		}
		else
		{
			return $webboard_post_level->image;
		}
	}
	
}
?>